package com.link.play.fit.ai.utils

import android.net.Uri
import androidx.core.net.toUri
import java.io.File

object Paths {
    const val ApkDir = "pkg"
    const val WallpaperDir = "wallpaper"
    const val ImgDir = "image"
    const val WidgetDir = "widget"
    const val RingToneDir = "RingToneFiles"

    /**
     * 应用存储文件的文件夹 (不需要读写权限)
     * @name 子文件夹名称
     */
    fun fileDir(name: String): File? {
        return runCatching {
            (AppX.context.getExternalFilesDir(name)?.apply {
                if (!exists() || !isDirectory) {
                    mkdirs()
                }
            }) ?: (AppX.context.filesDir?.let {
                File(it, name).apply {
                    if (!exists() || !isDirectory) {
                        mkdirs()
                    }
                }
            })
        }.getOrNull()
    }

    /**
     * 应用缓存文件夹 (不需要读写权限)
     * @name 子文件夹名称
     */
    fun cacheDir(name: String): File? {
        return runCatching {
            val dir = AppX.context.externalCacheDir ?: AppX.context.cacheDir
            dir?.let {
                File(it, name).apply {
                    if (!exists() || !isDirectory) {
                        mkdirs()
                    }
                }
            }
        }.getOrNull()
    }

    fun String.toUriCompat(): Uri {
        var uri = this.toUri()
        if (uri.scheme.isNullOrEmpty()) {
            uri = "file://$this".toUri()
        }
        return uri
    }
}